#include "setup.h"

void keyboard_init()
{
#ifdef SmartKB32_v1
      pinMode(M74HC165_CE, OUTPUT);
      digitalWrite(M74HC165_CE, LOW);
      pinMode(RTS, OUTPUT);
      digitalWrite(RTS, HIGH);
      pinMode(CTS, INPUT_PULLUP);
#endif
      pinMode(M74HC165_CLK, OUTPUT);
      digitalWrite(M74HC165_CLK, LOW);
      pinMode(M74HC165_MISO, INPUT);
      pinMode(M74HC165_PL, OUTPUT);
      digitalWrite(M74HC165_PL, HIGH);
}

uint8_t LEEDDD = 0b11010001; // 第一位：电源状态 第二位：LVGL 第三位：Dial 第四位：keymap1 第五位：keymap2 第六位：LVGL 第七位：读取指纹 第八位：电源状态

bool kb_state[32];
void read_74hc165d()
{
      pinMode(M74HC165_CLK, OUTPUT);
      pinMode(M74HC165_MISO, INPUT);
      digitalWrite(M74HC165_PL, LOW);
      bool read_kb_state[32];
      digitalWrite(M74HC165_PL, HIGH);
      for (int i = 0; i < 32; i++)
      {
            digitalWrite(M74HC165_CLK, LOW);
            bool a = digitalRead(M74HC165_MISO);
            (a == 1) ? a = 0 : a = 1;
            read_kb_state[i] = a;
            digitalWrite(M74HC165_CLK, HIGH);
      }
      kb_state[KEY_X7Y1] = read_kb_state[31];
      kb_state[KEY_X7Y2] = read_kb_state[30];
      kb_state[KEY_X7Y3] = read_kb_state[29];
      kb_state[KEY_X7Y4] = read_kb_state[28];
      kb_state[KEY_X7Y5] = read_kb_state[27];
      kb_state[KEY_X6Y5] = read_kb_state[26];
      kb_state[KEY_X6Y4] = read_kb_state[25];
      kb_state[KEY_X6Y3] = read_kb_state[24];
      kb_state[KEY_X5Y2] = read_kb_state[23];
      kb_state[KEY_X5Y3] = read_kb_state[22];
      kb_state[KEY_X5Y4] = read_kb_state[21];
      kb_state[KEY_X5Y5] = read_kb_state[20];
      kb_state[KEY_X4Y5] = read_kb_state[19];
      kb_state[KEY_X4Y4] = read_kb_state[18];
      kb_state[KEY_X4Y3] = read_kb_state[17];
      kb_state[KEY_X4Y2] = read_kb_state[16];
      kb_state[KEY_X4Y1] = read_kb_state[15];
      kb_state[KEY_X3Y1] = read_kb_state[14];
      kb_state[KEY_X3Y2] = read_kb_state[13];
      kb_state[KEY_X3Y3] = read_kb_state[12];
      kb_state[KEY_X3Y4] = read_kb_state[11];
      kb_state[KEY_X3Y5] = read_kb_state[10];
      kb_state[KEY_X2Y5] = read_kb_state[9];
      kb_state[KEY_X2Y4] = read_kb_state[8];
      kb_state[KEY_X2Y3] = read_kb_state[7];
      kb_state[KEY_X2Y2] = read_kb_state[6];
      kb_state[KEY_X2Y1] = read_kb_state[5];
      kb_state[KEY_X1Y1] = read_kb_state[4];
      kb_state[KEY_X1Y2] = read_kb_state[3];
      kb_state[KEY_X1Y3] = read_kb_state[2];
      kb_state[KEY_X1Y4] = read_kb_state[1];
      kb_state[KEY_X1Y5] = read_kb_state[0];
}

bool old_kb_state[32];

uint32_t click_time = 0;

uint32_t buzz_time = 0;     // BUZZ发声时长定时变量
#define BUZZ_TIME 40        // BUZZ发声时长
#define BUZZ_DELAY_TIME 100 // BUZZ两次发生时长间隔
bool BUZZ_INIT_ONOFF = 0;   // 是否启用BUZZ

bool key_state_up = 0;
bool DOUB_click_DOWN = 0;
void keyboard_run()
{
      read_74hc165d();
      if (main_cpu_sleep == 1)
      {
            for (uint8_t i = KEY_X2Y1; i <= KEY_X7Y1; i++)
            {
                  if (kb_state[i] != old_kb_state[i] and kb_state[i] == 1)
                  {
                        main_cpu_sleep = 0;
                        LEEDDD = 0b11010001;
                        Serial.printf("KBRD%03dFULL", 0);
                  }
            }
      }
      else
      {
            if (kb_state[KEY_X1Y1] == 1)
            {
                  if (old_kb_state[KEY_X1Y1] == 0)
                        Serial.printf("KBRD%03dFULL", KEY_DOWN_X1Y1);
                  for (uint8_t i = KEY_X2Y1; i <= KEY_X6Y5; i++)
                  {
                        if (kb_state[i] != old_kb_state[i] and kb_state[i] == 1)
                        {
                              DOUB_click_DOWN = 1;
                              Serial.printf("KBRD%03dFULL", i + DOUB_KEY_DOWN_X2Y1);
                        }
                        else if (kb_state[i] != old_kb_state[i] and kb_state[i] == 0)
                              Serial.printf("KBRD%03dFULL", i + DOUB_KEY_UP_X2Y1);
                  }
                  if (kb_state[KEY_X1Y5] != old_kb_state[KEY_X1Y5] and kb_state[KEY_X1Y5] == 0) // 设备睡眠控制
                  {
                        main_cpu_sleep = 1;
                        LEEDDD = 0b00000011;
                  }
            }
            else if (kb_state[KEY_X7Y1] == 1)
            {
                  if (old_kb_state[KEY_X7Y1] == 0)
                        Serial.printf("KBRD%03dFULL", KEY_DOWN_X7Y1);
                  for (uint8_t i = KEY_X7Y2; i <= KEY_X7Y5; i++)
                  {
                        if (kb_state[i] != old_kb_state[i] and kb_state[i] == 1)
                        {
                              DOUB_click_DOWN = 1;
                              Serial.printf("KBRD%03dFULL", DOUB_KEY_DOWN_X7Y2 + i - KEY_X7Y2);
                        }
                        else if (kb_state[i] != old_kb_state[i] and kb_state[i] == 0)
                              Serial.printf("KBRD%03dFULL", DOUB_KEY_UP_X7Y2 + i - KEY_X7Y2);
                  }
            }
            else if (kb_state[KEY_X1Y1] != old_kb_state[KEY_X1Y1])
            {

                  if (kb_state[KEY_X1Y1] == 0 and DOUB_click_DOWN == 0)
                  {
                        Serial.printf("KBRD%03dFULL", KEY_UP_X1Y1);
                        LEEDDD = LEEDDD ^ 0b00011000;
                  }
                  DOUB_click_DOWN = 0;
            }
            else if (kb_state[KEY_X7Y1] != old_kb_state[KEY_X7Y1])
            {
                  if (kb_state[KEY_X7Y1] == 0 and DOUB_click_DOWN == 0)
                  {
                        Serial.printf("KBRD%03dFULL", KEY_UP_X7Y1);
                        LEEDDD = LEEDDD ^ 0b01100000;
                  }
                  DOUB_click_DOWN = 0;
            }
            else
            {
                  for (uint8_t i = KEY_X2Y1; i <= KEY_X6Y5; i++)
                  {
                        if (kb_state[i] != old_kb_state[i])
                        {
                              if (kb_state[i] == 1) // 按下
                              {
                                    if (main_cpu_sleep == 1)
                                    {
                                          main_cpu_sleep = 0;
                                          LEEDDD = 0b11010001;
                                    }
                                    Serial.printf("KBRD%03dFULL", i + KEY_DOWN_X2Y1);
                                    if (BUZZ_INIT_ONOFF)
                                    {
                                          analogWrite(BUZZ, 200);
                                          buzz_time = millis();
                                    }
                              }
                              else // 抬起
                              {
                                    Serial.printf("KBRD%03dFULL", i + KEY_UP_X2Y1);
                              }
                        }
                  }
                  for (uint8_t i = KEY_X7Y2; i <= KEY_X7Y5; i++)
                  {
                        if (kb_state[i] != old_kb_state[i])
                        {
                              if (millis() - click_time >= 600)
                              {
                                    if (kb_state[i] == 1) // 按下
                                    {
                                          Serial.printf("KBRD%03dFULL", KEY_DOWN_X7Y2 + i - KEY_X7Y2);
                                          if (BUZZ_INIT_ONOFF)
                                          {
                                                analogWrite(BUZZ, 200);
                                                buzz_time = millis();
                                          }
                                          key_state_up = 1;
                                    }
                                    else // 抬起
                                    {
                                          key_state_up = 0;
                                          click_time = millis();
                                    }
                                    break;
                              }
                        }
                  }
            }
      }
      for (uint8_t i = 0; i < 32; i++)
      {
            old_kb_state[i] = kb_state[i];
      }
      if (millis() - buzz_time > BUZZ_TIME * 2 + BUZZ_DELAY_TIME)
      {
            analogWrite(BUZZ, 255);
      }
      else if (millis() - buzz_time > BUZZ_TIME + BUZZ_DELAY_TIME)
      {
            analogWrite(BUZZ, 200);
      }
      else if (millis() - buzz_time > BUZZ_TIME)
      {
            analogWrite(BUZZ, 255);
      }
}